package com.iwhalecloud.bss.kite.cucc.service.scene.query;

import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneMainOfferInstService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.dto.inst.OfferInstDTO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.validate.annotation.KiteNotBlank;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.manager.transaction.TransactionContext;
import com.iwhalecloud.bss.kite.manager.util.ManagerUtils;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 查询主套餐销售品实例场景
 *
 * @author G
 * @since 2019/6/11
 */
@Service("querySceneMainOfferInstService")
public class QuerySceneMainOfferInstService extends QuerySceneOfferInstService implements IQuerySceneMainOfferInstService {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    /***
     * 通过礼包实例获取主套餐实例列表
     *
     * @param sceneInstId
     * @param giftOfferInstId
     * @param custId 客户id，分片字段，必填
     * @return
     */
    @Override
    @Validated
    public DataResult<List<OfferInstDTO>> getMainOfferInstByGiftOfferInstId(@KiteNotBlank String sceneInstId,
        @KiteNotBlank String giftOfferInstId, @KiteNotBlank String custId) {
        // TODO 待替换调用的 api
        List<KiteOfferInst> kiteOfferInstList = querySceneInstService.listMainOfferInstsFromGiftOfferInst(custId,
            giftOfferInstId);
        List<OfferInstDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteOfferInstList)) {
            dtoList = CopyToDtoUtil.transOfferInstDtoList(kiteOfferInstList);
            // TODO 待确认 装箱
        }
        // else {
        // dtoList = queryMainOfferInstBO.getMainOfferInstByGiftOfferInstId(giftOfferInstId, custId);
        // checkAndPutOfferInst(offerInstDTO, queryMainOfferInstBO);
        // }
        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    /**
     * 获取可选包销售品实例所在的套餐销售品实例
     *
     * @param sceneInstId 场景实例id
     * @param additionalOfferId 可选包销售品id
     * @param custId 客户id
     * @return
     */
    @Override
    @Validated
    public DataResult<OfferInstDTO> getMainOfferInstByAdditionalOfferId(@KiteNotBlank String sceneInstId,
        @KiteNotBlank String additionalOfferId, String custId) {
        KiteOfferInst kiteOfferInst = querySceneInstService.getMainOfferInstByAdditionalOfferId(sceneInstId,
            additionalOfferId);
        OfferInstDTO offerInstDTO = null;
        if (kiteOfferInst != null) {
            offerInstDTO = CopyToDtoUtil.packageOfferInstDTO(kiteOfferInst, null);
            // TODO 待确认 装箱
        }
        // else {
        // offerInstDTO = queryMainOfferInstBO.getMainOfferInstByAdditionalOfferId(additionalOfferId, custId);
        // checkAndPutOfferInst(offerInstDTO, queryMainOfferInstBO);
        // }
        return ErrorConsts.SUCCESS.getResult(offerInstDTO);
    }

    /***
     * 场景使用--通过产品实例ID获取主套餐实例对象；
     * <li>注意：多主套餐实例返回是，用订购者和是否有效来区分</li>
     *
     * @param sceneInstId
     * @param prodInstId
     * @param custId 客户id，分片字段，必填
     * @return
     */
    @Override
    @Validated
    public DataResult<OfferInstDTO> getSceneMainOfferInstByProdInstId(@KiteNotBlank String sceneInstId,
        @KiteNotBlank String prodInstId, @KiteNotBlank String custId) {
        KiteOfferInst kiteOfferInst = querySceneInstService.getMainOfferInstByProdInstId(custId, prodInstId);
        OfferInstDTO offerInstDTO = null;
        if (kiteOfferInst != null) {
            offerInstDTO = CopyToDtoUtil.packageOfferInstDTO(kiteOfferInst, null);
        }
        return ErrorConsts.SUCCESS.getResult(offerInstDTO);
    }

    @Override
    public DataResult<List<OfferInstDTO>> listMainOfferInstsByProdInstId(String sceneInstId, String prodInstId,
        String custId) {
        List<OfferInstDTO> result = new ArrayList<>();
        List<KiteOfferInst> kiteOfferInsts = querySceneInstService.listMainOfferInstsByProdInstId(custId, prodInstId);
        for (KiteOfferInst kiteOfferInst : kiteOfferInsts) {
            result.add(CopyToDtoUtil.packageOfferInstDTO(kiteOfferInst, null));
        }
        return ErrorConsts.SUCCESS.getResult(result);
    }

    /***
     * 查询主套餐 -- 从缓存加载
     *
     * @param sceneInstId
     * @return
     */
    @Override
    @Validated
    public DataResult<OfferInstDTO> getMainOfferInst(@KiteNotBlank String sceneInstId) {
        KiteOfferInst kiteOfferInst = querySceneInstService.getMainOfferInst(sceneInstId);
        OfferInstDTO offerInstDTO = null;
        if (kiteOfferInst != null) {
            offerInstDTO = CopyToDtoUtil.packageOfferInstDTO(kiteOfferInst, null);
            // TODO 待确认 装箱
        }
        return ErrorConsts.SUCCESS.getResult(offerInstDTO);
    }

    /***
     * 查询主套餐 -- 从缓存加载
     *
     * @param sceneInstId
     * @return
     */
    @Override
    @Validated
    public DataResult<List<OfferInstDTO>> getMainOfferInstList(@KiteNotBlank String sceneInstId) {
        List<KiteOfferInst> kiteOfferInstList = querySceneInstService.listMainOfferInsts(sceneInstId);
        List<OfferInstDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteOfferInstList)) {
            dtoList = CopyToDtoUtil.transOfferInstDtoList(kiteOfferInstList);
            // TODO 待确认 装箱
        }
        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    /***
     * 查询有效的主套餐 -- 从缓存加载
     *
     * @param sceneInstId
     * @return
     */
    @Override
    @Validated
    public DataResult<List<OfferInstDTO>> getEffMainOfferInstList(@KiteNotBlank String sceneInstId) {
        KiteSceneInst sceneInst = TransactionContext.getContainer().getSceneInst(sceneInstId);
        Set<String> hideIds = sceneInst.getHideInsts();
        List<KiteOfferInst> kiteOfferInstList = querySceneInstService.listMainOfferInsts(sceneInstId);
        List<KiteOfferInst> effOfferInstList = new ArrayList<>();

        List<OfferInstDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteOfferInstList)) {
            for (KiteOfferInst offerInst: kiteOfferInstList) {
                if (!hideIds.contains(ManagerUtils.join(offerInst.getInstType(), offerInst.getOfferInstId()))) {
                    effOfferInstList.add(offerInst);
                }
            }
            dtoList = CopyToDtoUtil.transOfferInstDtoList(effOfferInstList);
            // TODO 待确认 装箱
        }
        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    @Override
    @Validated
    public DataResult<OfferInstDTO> getMainOfferInstByAccNum(String sceneInstId, String accNum, String custId) {
        KiteOfferInst kiteOfferInst = querySceneInstService.getMainOfferInstByAccNum(sceneInstId, accNum);
        OfferInstDTO offerInstDTO = null;
        if (kiteOfferInst != null) {
            offerInstDTO = CopyToDtoUtil.packageOfferInstDTO(kiteOfferInst, null);
        }
        return ErrorConsts.SUCCESS.getResult(offerInstDTO);
    }

    @Override
    public DataResult<List<InstAttrDTO>> listMainOfferInstAttrsByMainOfferInstId(String sceneInstId, String mainOfferInstId, String custId) {
        KiteOfferInst inst = querySceneInstService.getOfferInst(custId, mainOfferInstId);
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        List<KiteAttr> kiteAttrList = inst.listAttrs();
        operateInstAttrService.getKiteInstAttrs(inst, kiteAttrList, sceneInst);
        List<InstAttrDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteAttrList)) {
            dtoList = CopyToDtoUtil.transInstAttrDtoList(kiteAttrList);
        }

        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    @Override
    public KiteOfferInst getMainOfferInstByProdInstId(String custId, String prodInstId) {
        return querySceneInstService.getMainOfferInstByProdInstId(custId, prodInstId);
    }
}
